package com.chenshu.mycnblog.config;

import com.chenshu.mycnblog.common.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description：拦截器列表配置
 * User：chenshu
 * Date：2024-01-14
 * Time：11:31
 */
@Configuration
public class AppConfig implements WebMvcConfigurer {
    //定义不拦截的规则
    List<String> excludes = new ArrayList<String>() {{
        //放行static下的所有文件
        add("/**/*.html");
        add("/css/**");
        add("/js/**");
        add("/img/**");
        add("/editor.md/**");
        //放行登录和注册的接口
        add("/user/login");
        add("/user/reg");
        add("/article/detail");
        add("/user/userinfo");
        add(("/user/userstatus"));
        add("/article/list");
        add("/article/totalpage");
    }};

    @Autowired
    private LoginInterceptor loginInterceptor;

    //注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration =
                registry.addInterceptor(loginInterceptor);
        registration.addPathPatterns("/**");
        registration.excludePathPatterns(excludes);
    }
}
